﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyVoteMVC.Data;
using MyVoteMVC.Domain;
using MyVoteMVC.Common;
using MyVoteMVC.Common.Objects;
using MyVoteMVC.Data.Repositories;

namespace MyVoteMVC.Service
{
    public class VoteAuserService : IAuserService 
    {
        private const string EmailAddress = "@gzdi.com"; 

        IUnitOfWork unitOfWork;
        IRepository<VT_Users> usersRepository;
        IRepository<VT_Activities> activitiesPepository;
        IRepository<VT_Tickets> ticketsPepository;

        public VoteAuserService()
        {
            unitOfWork = Factory.GetInstance<IUnitOfWork>();
            usersRepository = unitOfWork.GetRepository<VT_Users>();
            activitiesPepository = unitOfWork.GetRepository<VT_Activities>();
            ticketsPepository = unitOfWork.GetRepository<VT_Tickets>();
        }
        public int AddUser(string userName, string userRealName, string userPassword, int IsAD, int isLeader)
        {
            string userEmail = userName + EmailAddress;
            int i = usersRepository.GetMany(o => o.User_Email == userEmail).Count();
            if (i > 0) return 2;

            VT_Users user = new VT_Users();
            user.User_Email = userEmail;
            user.User_ID = Guid.NewGuid().ToString();
            user.User_IfAdminValue = IsAD;
            user.User_Name = userName;
            user.User_Password = userPassword;
            user.User_RealName = userRealName;
            user.User_IfLeaderValue = isLeader;
            user.Created_Date = DateTime.Now;
            unitOfWork.BeginTransaction();
            usersRepository.Add(user);
            unitOfWork.CommitTransaction();
            return 1;
        }

        //编辑用户
        public bool EditUser(string userEmailName, string userName, string realName, string aUserEmailName)
        {
            userEmailName = userEmailName + EmailAddress;
            //aUserEmailName = aUserEmailName + EmailAddress;
            VT_Users user = usersRepository.GetMany(o => o.User_Email == userEmailName).FirstOrDefault();
            if (user != null)
            {
                unitOfWork.BeginTransaction();
                user.User_Name = userName;
                user.User_RealName = realName;
                user.Last_Update_By = aUserEmailName;
                user.Last_Update_Date = DateTime.Now;
                usersRepository.Update(user);
                unitOfWork.CommitTransaction();
                return true;
            }
            return false;
        }

        //userEmailName 传的是完整的邮箱名
        public bool EditUser(string userEmailName, string realName, int isAD, int isLeader)
        {
            VT_Users user = usersRepository.GetMany(o => o.User_Email == userEmailName).FirstOrDefault();
            if (user != null)
            {
                unitOfWork.BeginTransaction();           
                user.User_RealName = realName;
                user.User_IfAdminValue = isAD;
                user.User_IfLeaderValue = isLeader;
                user.Last_Update_Date = DateTime.Now;
                usersRepository.Update(user);
                unitOfWork.CommitTransaction();
                return true;
            }
            return false;
        }
        //userEmailName 传的是完整的邮箱名
        public bool EditUser(string userEmailName, string realName, int isAD, int isLeader, string userPassword)
        {
            VT_Users user = usersRepository.GetMany(o => o.User_Email == userEmailName).FirstOrDefault();
            if (user != null)
            {
                unitOfWork.BeginTransaction();
                user.User_RealName = realName;
                user.User_IfAdminValue = isAD;
                user.User_IfLeaderValue = isLeader;
                user.User_Password = userPassword;
                user.Last_Update_Date = DateTime.Now;
                usersRepository.Update(user);
                unitOfWork.CommitTransaction();
                return true;
            }
            return false;
        }
        //userEmailName 传的是完整的邮箱名
        public bool DeleteUser(string userEmailName)
        {
            //userEmailName = userEmailName + EmailAddress;
            VT_Users user = usersRepository.GetMany(o => o.User_Email == userEmailName).FirstOrDefault();
            if (user != null)
            {
                unitOfWork.BeginTransaction();
                usersRepository.Delete(user);
                unitOfWork.CommitTransaction();
                return true;
            }
            return false;
        }
      
        //strType0 为 普通用户，1为管理员 
        public bool SetPermissions(string userEmailName, string aUserEmailName, int iType)
        {
            userEmailName = userEmailName + EmailAddress;
            //aUserEmailName = aUserEmailName + EmailAddress;
            VT_Users user = usersRepository.GetMany(o => o.User_Email == userEmailName).FirstOrDefault();
            if (user != null)
            {    
                unitOfWork.BeginTransaction();
                user.User_IfAdminValue= iType;
                user.Last_Update_By = aUserEmailName;
                user.Last_Update_Date = DateTime.Now;
                usersRepository.Update(user);
                unitOfWork.CommitTransaction();
                return true;
            }
            return false;
        }

        public bool SetLeaderPermissions(string userEmailName, string aUserEmailName, int iType)
        {
            userEmailName = userEmailName + EmailAddress;
            //aUserEmailName = aUserEmailName + EmailAddress;
            VT_Users user = usersRepository.GetMany(o => o.User_Email == userEmailName).FirstOrDefault();
            if (user != null)
            {
                unitOfWork.BeginTransaction();
                user.User_IfLeaderValue = iType;
                user.Last_Update_By = aUserEmailName;
                user.Last_Update_Date = DateTime.Now;
                usersRepository.Update(user);
                unitOfWork.CommitTransaction();
                return true;
            }
            return false;
        }
        public VT_Users GetUserByID(string strUserID)
        {
            VT_Users v_User = usersRepository.GetMany(o => o.User_ID == strUserID).FirstOrDefault();
            return v_User;
        }

        //获取用户列表
        public IList<VT_Users> GetALLUsersList()
        {
            IList<VT_Users> v_UserList = usersRepository.GetAll().OrderBy(o=>o.User_ID).ToList();
            return v_UserList;
        }
        //获取普通用户列表
        public IList<VT_Users> GetOrdinaryUsersList()
        {
            IList<VT_Users> v_UserList = usersRepository.GetMany(o => (o.User_IfAdminValue == 0||o.User_IfAdminValue==null) && (o.User_IfLeaderValue == 0||o.User_IfLeaderValue==null)).OrderBy(o => o.User_ID).ToList();
            return v_UserList;
        }
        //获取领导用户列表
        public IList<VT_Users> GetLeaderUsersList()
        {
            IList<VT_Users> v_UserList = usersRepository.GetMany(o => o.User_IfLeaderValue == 1).OrderBy(o => o.User_ID).ToList();
            return v_UserList;
        }
        //获取管理员列表
        public IList<VT_Users> GetAdministratorList()
        {
            IList<VT_Users> v_UserList = usersRepository.GetMany(o => o.User_IfAdminValue == 1).OrderBy(o => o.User_ID).ToList();
            return v_UserList;
        }

        //获取某个人的票列表
        public IList<VT_Tickets> GetUersTickets(string userID)
        {
            IList<VT_Tickets> v_Tickets = ticketsPepository.GetMany(o => o.User_ID == userID).OrderBy(o => o.Ticket_No).ToList();
            return v_Tickets;
        }

        //获取某个人某个活动的票列表
        public VT_Tickets GetUersTickets(string userID, string activityID)
        {
            VT_Tickets v_Ticket = ticketsPepository.GetMany(o => o.User_ID == userID && o.Activity_ID == activityID).FirstOrDefault();
            return v_Ticket;
        }

        //获取某个活动的票
        public IList<VT_Tickets> GetActivityTickets(string activityID)
        {
            IList<VT_Tickets> v_Tickets = ticketsPepository.GetMany(o => o.Activity_ID== activityID).OrderBy(o => o.Ticket_No).ToList();
            return v_Tickets;
        }

        public IList<VT_Users> FindUsersList(string name)
        {
            var v_Users = from vUsers in usersRepository.QueryableEntity()
                          where vUsers.User_Email.Contains(name) || vUsers.User_Name.Contains(name) || vUsers.User_RealName.Contains(name)
                          select vUsers;
            IList<VT_Users> v_UsersList = v_Users.ToList();
            return v_UsersList;
        }

        public VT_Users GetUserByEmail(string strEmail)
        {
            VT_Users v_User = usersRepository.GetMany(o => o.User_Email == strEmail).FirstOrDefault();
            return v_User;
        }

        #region IDisposable Members

        private bool m_disposed;
        protected virtual void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                if (disposing)
                {
                    unitOfWork.Dispose();   
                }

                // Release unmanaged resources

                m_disposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~VoteAuserService()
        {
            Dispose(false);
        }
        #endregion
    }
}
